﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Weazel.Willows.Model
{
	public partial class Signal
		: Channel, ISignal
	{
		private bool refined = false;

		public override void Refine(Weazel.Willows.RefinedModel.Model model)
		{
			if (refined)
				return;

			refined = true;

			Module module = Parent as Module;
			Weazel.Willows.RefinedModel.Datapath datapath = module.Datapath;
			datapath.Signals.Add(LocalName, new Weazel.Gezel.Model.Signal(datapath, LocalName, Type.BitWidth, false));

			// refine the writer(s)
			if (this.input == null)
			{
				string message = 
					string.Format("No writer set for signal '{0}'", Name);

				throw new NullReferenceException(message);
			}

			this.input.Refine(model);
			
			// refine the reader(s)
			foreach (ISignal reader in outputs)
			{				
				reader.Refine(model);
			}
		}
	}
}
